package com.lebang.medicalInquiry.service;

import com.lebang.medicalInquiry.constant.BusinessEnum;
import com.lebang.medicalInquiry.constant.UserConstants;
import com.lebang.medicalInquiry.exception.BusinessException;
import com.lebang.medicalInquiry.mapper.*;
import com.lebang.medicalInquiry.model.dto.UserTokenDto;
import com.lebang.medicalInquiry.model.po.*;
import com.lebang.medicalInquiry.model.vo.ConsultationSheetInfoVo;
import com.lebang.medicalInquiry.model.vo.ConsultationSheetVo;
import com.lebang.medicalInquiry.model.vo.PhysicianInfoVo;
import com.lebang.medicalInquiry.model.vo.UserInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private ConsultationSheetMapper consultationSheetMapper;
    @Resource
    private UserPatientMapper userPatientMapper;
    @Resource
    private PhysicianMapper physicianMapper;

    @Resource
    private UserPatientCaseFileMapper userPatientCaseFileMapper;



    @Resource
    private ConsultationSheetScheduleMapper consultationSheetScheduleMapper;

    /**
     * 登录
     *
     * @param phone
     * @param ip
     * @return
     */
    public UserTokenDto login(String phone, String ip) {

        User user = userMapper.getByPhone(phone);
        if (user != null) {
            if (user.getState().equals(UserConstants.EXCEPTION)) {
                throw new BusinessException("账号已被封禁");
            }
        } else {
            user = new User();
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            user.setState(UserConstants.NORMAL);
            user.setPhoneNumber(phone);
            user.setRole(BusinessEnum.ROLE_USER.getName());
            user.setUuid(UUID.randomUUID().toString().replaceAll("-", ""));
            userMapper.insertSelective(user);
        }
        UserInfoVo userInfoVo = UserInfoVo.buildVo(user);
        userInfoVo.setRole(user.getRole());
        return UserTokenDto.builder().userVo(userInfoVo).build();
    }


    /**
     * 根据uuid查询就诊人列表
     *
     * @param uuid
     * @return
     */
    public List<UserPatient> getUserPatientList(String uuid, Integer page, Integer limit) {
        return userPatientMapper.getUserPatientList(uuid, null, (page - 1) * limit, limit);
    }

    /**
     * 根据uuid和患者id 查询就诊人上传的病例文件
     *
     * @param uuid
     * @param patientId
     * @return
     */
    public List<UserPatientCaseFile> getUserPatientCaseFileList(String uuid, Integer patientId, Integer page, Integer limit) {
        return userPatientCaseFileMapper.getUserPatientCaseFileList(uuid, patientId, (page - 1) * limit, limit);
    }

    /**
     * 根据uuid和关注状态查询关注
     *
     * @param uuid
     * @param classifyId  分类id
     * @param isAttention 是否关注
     * @param page
     * @param limit
     * @return
     */
    public List<PhysicianInfoVo> getAttPhysicianList(String name, String uuid, Integer classifyId, Integer organizationId, Integer isAttention, Integer page, Integer limit) {

        if (isAttention == 1) {
            return physicianMapper.getAttPhysicianList(name, uuid, classifyId, (page - 1) * limit, limit);
        } else {
            return physicianMapper.getPhysicianList(name, uuid, classifyId, organizationId, (page - 1) * limit, limit);
        }

    }

    /**
     * 修改就诊人信息
     *
     * @param uuid
     * @param userPatient
     */
    public void modifyUserPatient(String uuid, UserPatient userPatient) {
        if (userPatient.getId() != null) {
            UserPatient userPatient1 = userPatientMapper.selectByPrimaryKey(userPatient.getId());
            if (!userPatient1.getUuid().equals(uuid)) {
                throw new BusinessException("没有权限修改");
            }
            userPatientMapper.updateByPrimaryKeySelective(userPatient);
        } else {
            userPatient.setUuid(uuid);
            userPatientMapper.insertSelective(userPatient);
        }


    }

    /**
     * 修改就诊病例文件
     *
     * @param uuid
     * @param userPatientCaseFile
     */
    public UserPatientCaseFile modifyUserPatientCaseFile(String uuid, UserPatientCaseFile userPatientCaseFile) {
        if (userPatientCaseFile.getId() != null) {
            UserPatient userPatient1 = userPatientMapper.selectByPrimaryKey(userPatientCaseFile.getPatientId());
            if (!userPatient1.getUuid().equals(uuid)) {
                throw new BusinessException("没有权限修改");
            }
            userPatientCaseFileMapper.updateByPrimaryKeySelective(userPatientCaseFile);

        } else {

            userPatientCaseFileMapper.insertSelective(userPatientCaseFile);
        }
        return userPatientCaseFile;
    }

    /**
     * 删除就诊人员信息
     *
     * @param uuid
     * @param userPatientId
     */
    public void removeUserPatient(String uuid, Integer userPatientId) {
        UserPatient userPatient1 = userPatientMapper.selectByPrimaryKey(userPatientId);
        if (!userPatient1.getUuid().equals(uuid)) {
            throw new BusinessException("没有权限删除");
        }
        userPatientMapper.deleteByPrimaryKey(userPatientId);
        userPatientCaseFileMapper.removeByPatientId(userPatientId);

    }

    /**
     * 删除就诊病例
     *
     * @param uuid
     * @param physicianCaseFileId
     */
    public void removeUserPatientCaseFile(String uuid, Integer physicianCaseFileId) {
        UserPatientCaseFile userPatientCaseFile = userPatientCaseFileMapper.selectByPrimaryKey(physicianCaseFileId);
        UserPatient userPatient = userPatientMapper.selectByPrimaryKey(userPatientCaseFile.getPatientId());
        if (!userPatient.getUuid().equals(uuid)) {
            throw new BusinessException("没有权限删除");
        }
        userPatientCaseFileMapper.deleteByPrimaryKey(physicianCaseFileId);
    }


    /**
     * 查询用户的问诊单列表
     *
     * @param uuid
     * @return
     */
    public List<ConsultationSheetInfoVo> getUserConsultationSheetList(String uuid, Integer page, Integer limit) {

        return consultationSheetMapper.getConsultationSheetList(uuid, null, "", (page - 1) * limit, limit);
    }

    /**
     * 查询对应问诊单的进度
     *
     * @param uuid    用户uuid
     * @param sheetId 问诊单id
     * @return
     */
    public List<ConsultationSheetSchedule> getUserConsultationScheduleList(String uuid, Integer sheetId) {
        return consultationSheetMapper.getConsultationScheduleList(sheetId);
    }


    public ConsultationSheet getConsultationSheetById(Integer consultationSheetId) {
        return consultationSheetMapper.selectByPrimaryKey(consultationSheetId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addConsultationSheet(ConsultationSheet consultationSheet, ConsultationSheetSchedule consultationSheetSchedule) {
        // 同步问诊单状态
        consultationSheet.setScheduleState(consultationSheetSchedule.getScheduleState());
        consultationSheetMapper.updateByPrimaryKeySelective(consultationSheet);
        consultationSheetScheduleMapper.insertSelective(consultationSheetSchedule);
    }

    public void modifyUserInfo(User oldUser) {
        User newUser = new User();
        newUser.setId(oldUser.getId());
        newUser.setName(oldUser.getName());
        newUser.setSex(oldUser.getSex());
        newUser.setEmail(oldUser.getEmail());
        newUser.setNationality(oldUser.getNationality());
        newUser.setAge(oldUser.getAge());
        newUser.setHeight(oldUser.getHeight());
        newUser.setWeight(oldUser.getWeight());
        newUser.setDiseaseInfo(oldUser.getDiseaseInfo());
        newUser.setName(oldUser.getName());
        newUser.setHeadImg(oldUser.getHeadImg());
        newUser.setUuid(oldUser.getUuid());
//        userMapper.updateByPrimaryKeySelective(newUser);
        userMapper.updateByUuidKeySelective(newUser);
    }

    public ConsultationSheet getUserConsultationInfo(Integer sheetId) {
        return consultationSheetMapper.selectByPrimaryKey(sheetId);
    }

    public UserInfoVo getByUuid(String userId) {
        final User byUuid = userMapper.getByUuid(userId);
        if(byUuid==null){
            throw new BusinessException("用户uuid不存在");
        }
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setName(byUuid.getName());
        userInfoVo.setHeadImg(byUuid.getHeadImg());
        return userInfoVo;
    }
}
